package cn.iocoder.yudao.module.fengdong.smartelderly.service.address;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.address.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.address.AddressConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.address.AddressMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 我的地址 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class OphAddressServiceImpl implements OphAddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public Long createAddress(AddressCreateReqVO createReqVO) {
        // 插入
        AddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
        if (addressMapper.selectCount()==0) {
            address.setStatus(2L); //默认地址
        }else if(address.getStatus()!=null && address.getStatus()==2){
            addressMapper.updateStates(getLoginUserId());
        }

        addressMapper.insert(address);
        // 返回
        return address.getId();
    }

    @Override
    public void updateAddress(AddressUpdateReqVO updateReqVO) {
        // 校验存在
        validateAddressExists(updateReqVO.getId());
        if(updateReqVO.getStatus()!=null && updateReqVO.getStatus()==2){
            addressMapper.updateStates(getLoginUserId());
        }
        // 更新
        AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
        addressMapper.updateById(updateObj);
    }

    @Override
    public void deleteAddress(Long id) {
        // 校验存在
        validateAddressExists(id);
        // 删除
        addressMapper.deleteById(id);
    }

    private void validateAddressExists(Long id) {
        if (addressMapper.selectById(id) == null) {
            throw exception(ADDRESS_NOT_EXISTS);
        }
    }

    @Override
    public AddressDO getAddress(Long id) {
        return addressMapper.selectById(id);
    }

    @Override
    public List<AddressDO> getAddressList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return addressMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AddressDO> getAddressPage(AddressPageReqVO pageReqVO) {
        return addressMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AddressDO> getAddressList(AddressExportReqVO exportReqVO) {
        return addressMapper.selectList(exportReqVO);
    }

}
